package com.jzo2o.foundations.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.expcetions.BadRequestException;
import com.jzo2o.common.expcetions.CommonException;
import com.jzo2o.common.expcetions.ForbiddenOperationException;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.foundations.enums.FoundationStatusEnum;
import com.jzo2o.foundations.mapper.RegionMapper;
import com.jzo2o.foundations.mapper.ServeItemMapper;
import com.jzo2o.foundations.mapper.ServeMapper;
import com.jzo2o.foundations.model.domain.Region;
import com.jzo2o.foundations.model.domain.Serve;
import com.jzo2o.foundations.model.domain.ServeItem;
import com.jzo2o.foundations.model.dto.request.ServePageQueryReqDTO;
import com.jzo2o.foundations.model.dto.request.ServeUpsertReqDTO;
import com.jzo2o.foundations.model.dto.response.ServeResDTO;
import com.jzo2o.foundations.service.IServeService;
import com.jzo2o.mysql.utils.PageHelperUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;


/**
 * @author wdm
 */
@Service
public class ServeServiceImpl extends ServiceImpl<ServeMapper, Serve> implements IServeService {

    @Resource
    private ServeItemMapper serveItemMapper;
    @Resource
    private RegionMapper regionMapper;
    @Resource
    private ServeMapper serveMapper;

    /**
     * 根据区域id分页查询
     *
     * @param servePageQueryReqDTO
     * @return
     */
    @Override
    public PageResult<ServeResDTO> pageQuery(ServePageQueryReqDTO servePageQueryReqDTO) {
        return PageHelperUtils.selectPage(servePageQueryReqDTO,
                () -> baseMapper.queryListByRegionId(servePageQueryReqDTO.getRegionId()));
    }

    /**
     * 批量添加
     *
     * @param serveUpsertReqDTOList
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchAdd(List<ServeUpsertReqDTO> serveUpsertReqDTOList) {
        if (Objects.isNull(serveUpsertReqDTOList)) {
            throw new BadRequestException("数据为空");
        }
        List<Serve> serveList = new ArrayList<>();
        for (ServeUpsertReqDTO dto : serveUpsertReqDTOList) {
            ServeItem serveItem = serveItemMapper.selectById(dto.getServeItemId());
            if (ObjectUtil.isNull(serveItem) || serveItem.getActiveStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
                throw new ForbiddenOperationException("该服务未启用无法添加到区域下使用");
            }
            Integer count = lambdaQuery()
                    .eq(Serve::getRegionId, dto.getRegionId())
                    .eq(Serve::getServeItemId, dto.getServeItemId())
                    .count();
            if (count > 0) {
                throw new ForbiddenOperationException(serveItem.getName() + "服务已存在");
            }
            Serve serve = BeanUtil.toBean(dto, Serve.class);
            Region region = regionMapper.selectById(dto.getRegionId());
            serve.setCityCode(region.getCityCode());
            serveList.add(serve);
        }
        saveBatch(serveList);
    }

    /**
     * 价格修改
     *
     * @param id
     * @param price
     */
    @Override
    public void updatePriceById(Long id, BigDecimal price) {
        Serve serve = getById(id);
        if (Objects.isNull(serve)) {
            throw new ForbiddenOperationException("数据不存在");
        }
        serve.setPrice(price);
        updateById(serve);
    }

    /**
     * 区域服务上架
     *
     * @param id
     */
    @Override
    public void onSale(Long id) {
        Serve serve = getById(id);
        if (Objects.isNull(serve)) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        if (!(serve.getSaleStatus() == FoundationStatusEnum.INIT.getStatus() || serve.getSaleStatus() == FoundationStatusEnum.DISABLE.getStatus())) {
            throw new ForbiddenOperationException("草稿或下架状态方可上架");
        }
        ServeItem serveItem = serveItemMapper.selectById(serve.getServeItemId());
        if (ObjectUtil.isNull(serveItem)) {
            throw new ForbiddenOperationException("所属服务项不存在");
        }
        Integer activeStatus = serveItem.getActiveStatus();
        if (FoundationStatusEnum.ENABLE.getStatus() != activeStatus) {
            throw new ForbiddenOperationException("服务项为启用状态方可上架");
        }

        serve.setSaleStatus(FoundationStatusEnum.ENABLE.getStatus());
        updateById(serve);

    }

    /**
     * 区域服务下架
     *
     * @param id
     */
    @Override
    public void offSale(Long id) {
        Serve serve = getById(id);
        if (Objects.isNull(serve)) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        if (serve.getSaleStatus() != FoundationStatusEnum.ENABLE.getStatus()) {
            throw new ForbiddenOperationException("非上架状态不可下架");
        }
        serve.setSaleStatus(FoundationStatusEnum.DISABLE.getStatus());
        updateById(serve);
    }

    /**
     * 删除区域服务开发
     *
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        Serve serve = getById(id);
        if (Objects.isNull(serve)) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        if (serve.getSaleStatus() != FoundationStatusEnum.INIT.getStatus()) {
            throw new ForbiddenOperationException("非草稿状态不可删除");
        }
        removeById(id);

    }

    /**
     * 区域服务设置热门开发
     *
     * @param id
     */
    @Override
    public void onHot(Long id) {
        Serve serve = getById(id);
        if (Objects.isNull(serve)) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        if (serve.getIsHot() == FoundationStatusEnum.DISABLE.getStatus()) {
            throw new ForbiddenOperationException("已经是热门，不可重复操作");
        }
        serve.setIsHot(FoundationStatusEnum.DISABLE.getStatus());
        updateById(serve);
    }

    /**
     * 区域服务取消热门开发
     *
     * @param id
     */
    @Override
    public void offHot(Long id) {
        Serve serve = getById(id);
        if (Objects.isNull(serve)) {
            throw new ForbiddenOperationException("区域服务不存在");
        }
        if (serve.getIsHot() == FoundationStatusEnum.INIT.getStatus()) {
            throw new ForbiddenOperationException("已经是非热门，不可重复操作");
        }
        serve.setIsHot(FoundationStatusEnum.INIT.getStatus());
        updateById(serve);
    }

    /**
     * 根据区域id查询服务
     *
     * @param id
     * @return
     */
    @Override
    public Boolean selectByRegion(Long id) {
        List<Serve> list = lambdaQuery().eq(Serve::getRegionId, id)
                .list();
        if (Objects.isNull(list)) {
            return false;
        }
        int count = 0;
        for (Serve serve : list) {
            if (serve.getSaleStatus() == FoundationStatusEnum.ENABLE.getStatus()) {
                count++;
            }
        }
        if (count > 0) {
            return true;
        }
        return false;
    }

    /**
     * 根据服务项id和状态查询区域服务个数
     *
     * @param id
     * @return
     */
    @Override
    public int queryServeCountByItemId(Long id, int status) {
        return serveMapper.queryServeCountByItemId(id,status);
    }
}
